package buco.integrity.typehierarchy;

import java.util.*;

public class ClassGraph {
	private HashMap<String, ClassNode> nodes; 
	
	public ClassGraph() { 
		nodes = new HashMap<String, ClassNode>(); 
	}
	
	public void insert(String parentClass, String subClass) { 
		ClassNode parent = null; 
		ClassNode child = null;
		
		if(nodes.containsKey(parentClass)) {
			parent = nodes.get(parentClass);
		} else { 
			parent = new ClassNode(parentClass); 
			nodes.put(parentClass, parent); 
		}
		
		if(nodes.containsKey(subClass)) { 
			child = nodes.get(subClass);			
		} else { 
			child = new ClassNode(subClass); 
			nodes.put(subClass, child);
		}
		
		parent.addSubClass(child); 		
	}
	
	public boolean isSubType(String subClass, String baseClass) { 
		if(!nodes.containsKey(baseClass)) 
			return false; 
		
		if(!nodes.containsKey(subClass))
			return false;
		
		ClassNode parent = nodes.get(baseClass);
		ClassNode child = nodes.get(subClass);
		
		return new ClassGraphWalker().findConnection(parent, child);
	}
	
	
	
}
